package com.bingo.study;

import java.io.File;
import java.io.FileReader;
import java.nio.file.FileSystems;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

public class HighLightSearch {

	public static void main(String[] args) {
		Index newIndex = new Index();
		newIndex.index();
		Search newSearch = new Search();
		newSearch.search("defaultTopicQueueNums");
	}

}

class Index {
	//建立索引
	public void index() {
		IndexWriter indexWriter = null;
		try {
			//
			Directory directory = FSDirectory.open(FileSystems.getDefault().getPath("F:/lucene/index"));
			//创建IndexWrier
			Analyzer analyzer = new StandardAnalyzer();
			IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
			indexWriter = new IndexWriter(directory,indexWriterConfig);
			indexWriter.deleteAll();//清除以前的index
			//要搜索的file路径
			File dFile = new File("F:/lucene/data");
			File[] files = dFile.listFiles();
			for(File file : files) {
				//创建Document对象
				Document document = new Document();
				//为Document添加field
				document.add(new Field("content", new FileReader(file), TextField.TYPE_NOT_STORED));
                document.add(new Field("filename", file.getName(), TextField.TYPE_STORED));
                document.add(new Field("filepath", file.getAbsolutePath(), TextField.TYPE_STORED));
                // 5、通过IndexWriter添加文档到索引中
                indexWriter.addDocument(document);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if(indexWriter != null) {
					indexWriter.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}

/**
 *  创建Directory
    创建IndexReader    
    根据IndexReader创建IndexSearch        
    创建搜索的Query 
    根据searcher搜索并且返回TopDocs  
    根据TopDocs获取ScoreDoc对象        
    根据searcher和ScoreDoc对象获取具体的Document对象          
    根据Document对象获取需要的值
 * 
 */
class Search {
	//搜索
	public void search(String keyWord) {
		DirectoryReader directoryReader = null;
		try {
			//创建Directory
			Directory directory = FSDirectory.open(FileSystems.getDefault().getPath("F:/lucene/index"));
			//创建indexReader
			directoryReader = DirectoryReader.open(directory);
			//创建indexSearch
			IndexSearcher indexSearcher = new IndexSearcher(directoryReader);
			//创建索引的query
			Analyzer analyzer = new StandardAnalyzer();
			//创建parser来确定要搜索的文件内容，第一个参数为搜索的域
			QueryParser queryParser = new QueryParser("content", analyzer);
			//
			Query query = queryParser.parse(keyWord);
			//根据searcher搜索并且返回topDocs
			TopDocs topDocs = indexSearcher.search(query, 10);
			System.out.println("查找到的文档共有："+topDocs.totalHits);
			//根据topDocs获取scoreDoc对象
			ScoreDoc[] scoreDocs = topDocs.scoreDocs;
			for(ScoreDoc scoreDoc : scoreDocs) {
				Document document = indexSearcher.doc(scoreDoc.doc);
				System.out.println(document.get("filename")+" "+document.get("filepath"));
				
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (directoryReader != null) {
					directoryReader.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}